package com.sevennotes.classtable.ui.pages.saverecordpage

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sevennotes.classtable.model.ClassItem
import com.sevennotes.classtable.model.ClassItemWithName
import com.sevennotes.classtable.ui.pages.components.ItemCell
import com.sevennotes.classtable.ui.pages.components.NumChanger
import com.sevennotes.classtable.ui.pages.components.myDatePicker
import com.sevennotes.classtable.ui.pages.main_screen.ClassRecordState
import com.sevennotes.classtable.ui.pages.main_screen.SaveClassRecordState
import com.sevennotes.classtable.utils.TimeUtil
import java.util.*

@Composable
fun SaveClassRecordState(
    saveClassRecordState: SaveClassRecordState,
    updateClassRecord: (ClassRecordState) -> Unit,
    updateCurrentDay: (Date) -> Unit
) {
    val recordList = saveClassRecordState.recordList
    val currentDate = saveClassRecordState.currentDate
    val dateStr = TimeUtil.formatDate(currentDate, "yyyy年MM月dd日 EEEE")
    val context = LocalContext.current
    val datePicker = myDatePicker(currentDate, context) { updateCurrentDay(it) }
    Column {
        ItemCell {
            Text(
                modifier = Modifier.weight(1f),
                text = dateStr,
                textAlign = TextAlign.Center,
                fontSize = 24.sp
            )
            IconButton(onClick = {
                datePicker.show()
            }) { Icon(imageVector = Icons.Filled.DateRange, contentDescription = null) }
        }
        LazyColumn(
            modifier = Modifier.fillMaxWidth()
        ) {
            items(items = recordList) { item ->
                var noteOpen by remember { mutableStateOf(false) }
                RecordCell(
                    modifier = Modifier
                        .height(80.dp)
                        .clickable { noteOpen = !noteOpen },
                    item = item,
                    onCheckedChange = { updateClassRecord(item.copy(shouldSave = it)) },
                    onPointChanged = { updateClassRecord(item.copy(gpChange = it)) }
                )
                Spacer(modifier = Modifier.height(5.dp))
                AnimatedVisibility(visible = noteOpen) {
                    ItemCell {
                        OutlinedTextField(
                            modifier = Modifier.fillMaxWidth(),
                            value = item.note,
                            label = { Text(text = "备注信息") },
                            onValueChange = { updateClassRecord(item.copy(note = it)) }
                        )
                    }
                    Spacer(modifier = Modifier.height(5.dp))
                }
            }
        }
    }
}

@Composable
fun RecordCell(
    modifier: Modifier = Modifier,
    item: ClassRecordState,
    onCheckedChange: (Boolean) -> Unit,
    onPointChanged: (Int) -> Unit
) {
    Row(
        modifier = modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Checkbox(checked = item.shouldSave, onCheckedChange = onCheckedChange)
        Column(
            modifier = Modifier.weight(1f)
        ) {
            Text(
                text = TimeUtil.conversionTimeToString(
                    item.classItem.classItem.startTime,
                    item.classItem.classItem.duration,
                    0,
                    false
                )
            )
            Text(text = item.classItem.studentName, fontSize = 20.sp)
        }

        NumChanger(pointValue = item.gpChange, onPointChanged = onPointChanged)
    }
}

@Preview(showBackground = true)
@Composable
fun CellPrev() {
    val state = ClassRecordState(
        classItem = ClassItemWithName(ClassItem(), studentName = "abc"),
        gpChange = 3,
        note = "备注",
    )
    RecordCell(item = state, onCheckedChange = {}, onPointChanged = {})
}